package com.j2eefast.flowable.bpm.controller;

import com.j2eefast.common.core.business.annotaion.BussinessLog;
import com.j2eefast.common.core.controller.BaseController;
import com.j2eefast.common.core.enums.BusinessType;
import com.j2eefast.common.core.utils.ResponseData;
import com.j2eefast.common.core.utils.ValidatorUtil;
import com.j2eefast.flowable.bpm.entity.BpmBidLegalPersonEntity;
import com.j2eefast.flowable.bpm.entity.BpmProcessDefinitionEntity;
import com.j2eefast.flowable.bpm.entity.BpmTaskFromEntity;
import com.j2eefast.flowable.bpm.service.*;
import com.j2eefast.framework.annotation.RepeatSubmit;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.sys.service.SysUserService;
import com.j2eefast.framework.utils.UserUtils;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 劳务、购销合同盖章申请表
 * @author summer
 * @date 2021-06-4 22:19
 */
@Controller
@RequestMapping("/bpm/bidlegalperson")
public class BpmBidLegalPersonController extends BaseController{

    private String prefix = "modules/bpm/bidlegalperson";

    /**
     * 必须注入实例关联表单服务
     */
    @Autowired
    private BpmTaskFromService bpmTaskFromService;
    @Autowired
    private BpmBidLegalPersonService bidLegalPersonService;
    @Autowired
    private ProcdefService procdefService;
    @Autowired
    private FlowableCommentService flowableCommentService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 定义关联表单申请表单URL对应此处
     */
    @GetMapping("/add/{id}")
    @RequiresPermissions("bpm:form:add")
    public String add(@PathVariable("id") Long id, ModelMap mmp){
        //通过页面传入的表单ID查询表单关联信息
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getTaskFromById(id);
        mmp.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmp.put("proDef", proDef);
        mmp.put("user", UserUtils.getUserInfo());
        return prefix + "/add";
    }

    /**
     * 新增
     */
    @RepeatSubmit
    @BussinessLog(title = "相关文件盖章申请表", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @RequiresPermissions("bpm:form:add")
    @ResponseBody
    public ResponseData addForm(@Validated BpmBidLegalPersonEntity bpmBidLegalPersonEntity){
        //校验参数
        ValidatorUtil.validateEntity(bpmBidLegalPersonEntity);
//        设置申请人id
        bpmBidLegalPersonEntity.setCreateBy(bpmBidLegalPersonEntity.getLaunchName());
        SysUserEntity application= sysUserService.getSysById(Long.parseLong(bpmBidLegalPersonEntity.getApplicantId()));
        bpmBidLegalPersonEntity.setApplicant(application.getName());
        return bidLegalPersonService.addForm(bpmBidLegalPersonEntity);
    }

    /**
     * 新增并悬挂
     */
    @RepeatSubmit
    @BussinessLog(title = "悬挂开标信息登记表", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/saveAsDraft", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("bpm:form:add")
    public ResponseData saveAsDraft(@Validated BpmBidLegalPersonEntity bpmBidLegalPersonEntity){
        bpmBidLegalPersonEntity.setCreateBy(bpmBidLegalPersonEntity.getLaunchName());
        SysUserEntity application= sysUserService.getSysById(Long.parseLong(bpmBidLegalPersonEntity.getApplicantId()));
        bpmBidLegalPersonEntity.setApplicant(application.getName());
        //校验参数
        ValidatorUtil.validateEntity(bpmBidLegalPersonEntity);
        return bidLegalPersonService.saveAsDraft(bpmBidLegalPersonEntity);
    }

    /**
     * 表单详情
     * @param taskId
     * @param businessKey
     * @param mmap
     * @return
     */
    @GetMapping("/view")
    @RequiresPermissions("bpm:form:view")
    public String view(@RequestParam(value="taskId", required=true)String taskId,
                       @RequestParam(value="businessKey", required=true)String businessKey,
                       ModelMap mmap){
        BpmBidLegalPersonEntity bpmBidLegalPersonEntity = bidLegalPersonService.getFormById(businessKey);
        mmap.put("bpmForm", bpmBidLegalPersonEntity);
        mmap.put("taskId", taskId);
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getById(bpmBidLegalPersonEntity.getFormId());
        mmap.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmap.put("proDef", proDef);
        mmap.put("comment", flowableCommentService.getFlowCommentVosByProcessInstanceId(bpmBidLegalPersonEntity.getProcessInstanceId()));
        return prefix + "/view";
    }

    @RepeatSubmit
    @BussinessLog(title = "相关文件盖章申请表", businessType = BusinessType.INSERT)
    @RequestMapping(value = "/send", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions("bpm:form:add")
    public ResponseData sendForm(@RequestParam(value="formDraftId", required=true)String formDraftId){
        return bidLegalPersonService.sendForm(formDraftId);
    }

    /**
     * 修改
     */
    @GetMapping("/edit")
    @RequiresPermissions("bpm:form:add")
    public String edit(@RequestParam(value="taskId", required=true)String taskId,
                       @RequestParam(value="businessKey", required=true)String businessKey,
                       ModelMap mmap){
        BpmBidLegalPersonEntity bpmOaForm = bidLegalPersonService.getFormById(businessKey);
        /*if(bpmOaForm.getProcessInstanceId()==null){
            //修改草稿时，没有processInstanceId的值
            bpmOaForm.setProcessInstanceId("");
        }*/
        mmap.put("bpmForm", bpmOaForm);
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getById(bpmOaForm.getFormId());
        mmap.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmap.put("proDef", proDef);
        mmap.put("taskId", taskId);
        return prefix + "/edit";
    }

    /**
     * 修改待发流程表单
     */
    @RepeatSubmit
    @RequiresPermissions("bpm:form:add")
    @BussinessLog(title = "编辑开标信息登记表", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData editForm(BpmBidLegalPersonEntity bpmBidLegalPersonEntity){
        ValidatorUtil.validateEntity(bpmBidLegalPersonEntity);
        //        设置申请人id
        bpmBidLegalPersonEntity.setCreateBy(bpmBidLegalPersonEntity.getLaunchName());
        SysUserEntity application= sysUserService.getSysById(Long.parseLong(bpmBidLegalPersonEntity.getApplicantId()));
        bpmBidLegalPersonEntity.setApplicant(application.getName());
        return bidLegalPersonService.editForm(bpmBidLegalPersonEntity)? success(): error("修改失败!");
    }


    @GetMapping("/flow")
    @RequiresPermissions("bpm:form:add")
    public String flow(@RequestParam(value="taskId", required=true)String taskId,
                       @RequestParam(value="businessKey", required=true)String businessKey,
                       ModelMap mmap){
        BpmBidLegalPersonEntity bpmOaForm = bidLegalPersonService.getFormById(businessKey);
        mmap.put("bpmForm", bpmOaForm);
        mmap.put("taskId", taskId);
        BpmTaskFromEntity bpmTaskFrom = bpmTaskFromService.getById(bpmOaForm.getFormId());
        mmap.put("bpmTaskFrom", bpmTaskFrom);
        BpmProcessDefinitionEntity proDef = procdefService.getById(bpmTaskFrom.getProcessDefinitionKey());
        mmap.put("proDef", proDef);
        mmap.put("comment", flowableCommentService.getFlowCommentVosByProcessInstanceId(bpmOaForm.getProcessInstanceId()));
        return prefix + "/flow";
    }

}